<?php
/**
 * Created by PhpStorm.
 * User: maple
 * Date: 17-11-24
 * Time: 下午2:29
 */

namespace AppBundle\Services;

use AppBundle\Util\DB2Util;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection;

class DB2Service extends BaseService
{
    /** @var  PDOConnection */
    private $dbconn;
    /** @var  DB2Util */
    private $dBUtil;

    /**
     * DB2Service constructor.
     * @param Connection $dbconn
     * @param LoggerInterface $logger
     */
    public function __construct($dbconn, $logger)
    {
        $this->dbconn = $dbconn;
        $this->setLogger($logger);
        $this->dBUtil = new DB2Util($this->dbconn, $logger);
    }

    /**
     * @param $sql
     * @param array $body
     * @param array $whereParams
     * @param array $orderParams
     * @return array
     * @throws \Exception
     */
    public function exec($sql, $body = array(), $whereParams = array(), $orderParams = array())
    {
        try {
            $this->dbconn->beginTransaction();
            $result = $this->dBUtil->getResult($sql, $body, $whereParams, $orderParams);
            $this->dbconn->commit();
            return $result;
        } catch (\Exception $ex) {
            $this->dbconn->rollBack();
            $this->logError('[DB2Service exec] has excepition, message: ' . $ex->getMessage());
            throw $ex;
        }
    }
}